package com.sunday.volunteer.admin.ui.index;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.RatingBar;
import android.widget.TextView;

import com.squareup.picasso.Picasso;
import com.sunday.common.event.EventBus;
import com.sunday.common.imageselector.MultiImageSelectorActivity;
import com.sunday.common.model.ResultDO;
import com.sunday.common.utils.ImageUtils;
import com.sunday.common.utils.ToastUtils;
import com.sunday.member.base.BaseActivity;
import com.sunday.member.utils.SharePerferenceUtils;
import com.sunday.member.utils.UploadUtils;
import com.sunday.volunteer.admin.BaseApp;
import com.sunday.volunteer.admin.R;
import com.sunday.volunteer.admin.entity.Member;
import com.sunday.volunteer.admin.entity.UploadFileBack;
import com.sunday.volunteer.admin.event.UpdateMemberEvent;
import com.sunday.volunteer.admin.http.VolunteerClient;
import com.sunday.volunteer.admin.ui.login.ForgetPwdActivity;
import com.sunday.volunteer.admin.utils.CircleImageView;

import java.util.ArrayList;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import okhttp3.RequestBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

/*
* 组织信息
* */
public class GroupInfoActivity extends BaseActivity {

    @Bind(R.id.title_view)
    TextView titleView;
    @Bind(R.id.comment_ratingStar)
    RatingBar commentRatingStar;
    @Bind(R.id.tv_type)
    TextView tvType;
    @Bind(R.id.tv_name)
    TextView tvName;
    @Bind(R.id.tv_for)
    TextView tvFor;
    @Bind(R.id.tv_team)
    TextView tvTeam;
    @Bind(R.id.tv_phone)
    TextView tvPhone;
    @Bind(R.id.img_logo)
    CircleImageView imgLogo;

    private Member member;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_group_info);
        ButterKnife.bind(this);
        titleView.setText("组织信息");
        initView();
    }

    private void initView() {
        member = BaseApp.getInstance().getMember();
        if (!TextUtils.isEmpty(member.getLogo())) {
            Picasso.with(mContext)
                    .load(member.getLogo())
                    .resize(120, 120)
                    .placeholder(R.mipmap.person_default)
                    .error(R.mipmap.person_default)
                    .into(imgLogo);
        }
        commentRatingStar.setRating(member.getScore());
        tvName.setText(member.getName());
        tvType.setText(member.getGroupType());
        tvFor.setText(member.getServiceArea());
        tvTeam.setText(member.getLimitNumber() + "人");
        tvPhone.setText(member.getMobile());
    }

    @OnClick({R.id.img_logo, R.id.l_pwd})
    public void onClick(View v) {
        int id = v.getId();
        switch (id) {
            case R.id.img_logo:
                toImg();
                break;
            case R.id.l_pwd:
                intent = new Intent(mContext, ForgetPwdActivity.class);
                intent.putExtra("resetPwd",true);////是否是修改密码,true:修改密码,false:忘记密码
                startActivity(intent);
                break;
        }
    }

    private void toImg() {
        intent = new Intent(mContext, MultiImageSelectorActivity.class);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SHOW_CAMERA, true);
        intent.putExtra(MultiImageSelectorActivity.EXTRA_SELECT_MODE, MultiImageSelectorActivity.MODE_SINGLE);
        startActivityForResult(intent, 101);
    }

    private ArrayList<String> mSelectPath = new ArrayList<>();

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK) {
            String imgs = null;
            mSelectPath.clear();
            mSelectPath = data.getStringArrayListExtra(MultiImageSelectorActivity.EXTRA_RESULT);
            if (mSelectPath != null) {
                imgs = ImageUtils.getCompressImagePath(mSelectPath.get(0));
                uploadOneImg(UploadUtils.getRequestBody(imgs));
            }
            Picasso.with(mContext).load(imgs)
                    .resize(120, 120)
                    .error(R.drawable.person_default)
                    .placeholder(R.drawable.person_default)
                    .into(imgLogo);
        } else {
            initView();
        }
    }

    private void uploadOneImg(final RequestBody requestBody) {
        showLoadingDialog(0);
        Call<ResultDO<UploadFileBack>> call = VolunteerClient.getVolunteerAdapter().uploadSingleFile(requestBody);
        call.enqueue(new Callback<ResultDO<UploadFileBack>>() {
            @Override
            public void onResponse(Call<ResultDO<UploadFileBack>> call, Response<ResultDO<UploadFileBack>> response) {
                if (response.body() == null || isFinish) {
                    return;
                }
                if (response.body().getCode() == 0) {
                    UploadFileBack uploadFileBack = response.body().getResult();
                    if (uploadFileBack != null) {
                        updateInfo(uploadFileBack.getHalfPath());
                    } else {
                        dissMissDialog();
                        ToastUtils.showToast(mContext, "图片上传失败");
                    }
                } else {
                    dissMissDialog();
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<UploadFileBack>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    private void updateInfo(String logo) {
        Call<ResultDO<Member>> call
                = VolunteerClient.getVolunteerAdapter().updateLogo(member.getId(), logo);
        call.enqueue(new Callback<ResultDO<Member>>() {
            @Override
            public void onResponse(Call<ResultDO<Member>> call, Response<ResultDO<Member>> response) {
                dissMissDialog();
                if (response.body() != null && response.body().getCode() == 0) {
                    member = response.body().getResult();
                    SharePerferenceUtils.getIns(mContext).saveOAuth(member);
                    initView();
                    EventBus.getDefault().post(new UpdateMemberEvent());
                } else {
                    ToastUtils.showToast(mContext, response.body().getMessage());
                }
            }

            @Override
            public void onFailure(Call<ResultDO<Member>> call, Throwable t) {
                dissMissDialog();
                ToastUtils.showToast(mContext, "网络错误");
            }
        });
    }

    public void onEvent(UpdateMemberEvent event) {
        initView();
    }

    @OnClick({R.id.l_type, R.id.l_name, R.id.l_for, R.id.l_team})
    public void update(View view) {
        intent = new Intent(mContext,UpdateMemberInfoActivity.class);
        switch (view.getId()) {
            case R.id.l_type:
                intent.putExtra("update_type",1);
                break;
            case R.id.l_name:
                intent.putExtra("update_type",2);
                break;
            case R.id.l_for:
                intent.putExtra("update_type",3);
                break;
            case R.id.l_team:
                intent.putExtra("update_type",4);
                break;
        }
        startActivity(intent);
    }
}
